package com.ffmpeg.learn;

import static androidx.core.content.PackageManagerCompat.LOG_TAG;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import android.view.Window;
import android.view.WindowManager;
import android.content.pm.ActivityInfo;

public class MainActivity extends AppCompatActivity {
    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉标题栏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏，隐藏状态
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
        );
        //屏幕为横屏
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);

        //将文件拷贝到sdcard中
        AssetCopyer assetCopyer = new AssetCopyer(MainActivity.this.getApplicationContext());
        try {
            assetCopyer.copy();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        tv.setText(stringFromJNI());
    }

    @Override
    protected void onStart() {
        super.onStart();
        //从sdcard中读取文件
//        /storage/emulated/0/Android/data/com.ffmpeg.learn/files/1080.mp4
//        /data/user/0/com.ffmpeg.learn/files/1080.mp4
//        File path = getExternalFilesDir(null);
//        File path = getFilesDir();
//        String file = path +"/1080.mp4";
//        Open( file,this );
//        openFile();
    }

    private void openFile() {
        File file = new File(getExternalFilesDir(null), "10801.mp4");
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                Log.i(LOG_TAG, line);
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "Failed to open file", e);
        }
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    public native boolean Open(String url, Object handle);
}